package com.ruoyi.web.controller.business;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.HurtDoctorAdvice;
import com.ruoyi.business.service.IHurtDoctorAdviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医师医嘱Controller
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RestController
@RequestMapping("/business/doctorAdvice")
public class HurtDoctorAdviceController extends BaseController
{
    @Autowired
    private IHurtDoctorAdviceService hurtDoctorAdviceService;

    /**
     * 查询医师医嘱列表
     */
    @PreAuthorize("@ss.hasPermi('business:doctorAdvice:list')")
    @GetMapping("/list")
    public TableDataInfo list(HurtDoctorAdvice hurtDoctorAdvice)
    {
        startPage();
        List<HurtDoctorAdvice> list = hurtDoctorAdviceService.selectHurtDoctorAdviceList(hurtDoctorAdvice);
        return getDataTable(list);
    }

    /**
     * 导出医师医嘱列表
     */
    @PreAuthorize("@ss.hasPermi('business:doctorAdvice:export')")
    @Log(title = "医师医嘱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HurtDoctorAdvice hurtDoctorAdvice)
    {
        List<HurtDoctorAdvice> list = hurtDoctorAdviceService.selectHurtDoctorAdviceList(hurtDoctorAdvice);
        ExcelUtil<HurtDoctorAdvice> util = new ExcelUtil<HurtDoctorAdvice>(HurtDoctorAdvice.class);
        util.exportExcel(response, list, "医师医嘱数据");
    }

    /**
     * 获取医师医嘱详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:doctorAdvice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hurtDoctorAdviceService.selectHurtDoctorAdviceById(id));
    }

    /**
     * 新增医师医嘱
     */
    @PreAuthorize("@ss.hasPermi('business:doctorAdvice:add')")
    @Log(title = "医师医嘱", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HurtDoctorAdvice hurtDoctorAdvice)
    {
        return toAjax(hurtDoctorAdviceService.insertHurtDoctorAdvice(hurtDoctorAdvice));
    }

    /**
     * 修改医师医嘱
     */
    @PreAuthorize("@ss.hasPermi('business:doctorAdvice:edit')")
    @Log(title = "医师医嘱", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HurtDoctorAdvice hurtDoctorAdvice)
    {
        return toAjax(hurtDoctorAdviceService.updateHurtDoctorAdvice(hurtDoctorAdvice));
    }

    /**
     * 删除医师医嘱
     */
    @PreAuthorize("@ss.hasPermi('business:doctorAdvice:remove')")
    @Log(title = "医师医嘱", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hurtDoctorAdviceService.deleteHurtDoctorAdviceByIds(ids));
    }


    /**
     * 医师为患者添加医嘱任务
     * @param hurtDoctorAdvice
     * @return
     */
    @PostMapping("/addAdviceForPatient")
    public AjaxResult addAdviceForPatient(@RequestBody HurtDoctorAdvice hurtDoctorAdvice){
        Long doctorId = SecurityUtils.getUserId();
        hurtDoctorAdvice.setDoctorId(doctorId);
        return toAjax(hurtDoctorAdviceService.insertHurtDoctorAdvice(hurtDoctorAdvice));
    }

    /**
     * 医师获取患者医嘱列表
     * @param patientId
     * @return
     */
    @GetMapping("/getAdviceByPatient")
    public AjaxResult getAdviceByPatient(Long patientId){
        Long doctorId = SecurityUtils.getUserId();
        List<Map<String, Object>> list = hurtDoctorAdviceService.getAdviceByPatient(doctorId, patientId);
        return AjaxResult.success(list);
    }

    /**
     * 患者获取自己的医嘱列表
     * @return
     */
    @GetMapping("/getAdviceListByPatient")
    public AjaxResult getAdviceListByPatient(){
        Long patientId = SecurityUtils.getUserId();
        List<Map<String, Object>> list = hurtDoctorAdviceService.getAdviceListByPatient(patientId);
        return AjaxResult.success(list);
    }

    /**
     * 获取医嘱详情
     * @return
     */
    @GetMapping("/getAdviceDetailById")
    public AjaxResult getAdviceDetailById(Long id){
        Map<String, Object> map = hurtDoctorAdviceService.getAdviceDetailById(id);
        if (map!=null){
            return AjaxResult.success(map);
        }else {
            return AjaxResult.success(new HashMap<>());
        }
    }
}
